package com.ddy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddy.common.R;
import com.ddy.entity.Movie;
import com.ddy.mapper.MovieMapper;
import com.ddy.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {
    @Autowired
    private MovieService movieService;

    //NOTE:
    //Cacheable先查询缓存，不可访问result，若缓存未命中，则执行方法。
    //CachePut将方法返回结果缓存，在函数结束后运行，可访问result.
    @Override
    @Cacheable(value = "moviesByAuth", key = "#movie.author")
    public List<Movie> getMoviesByAuth(Movie movie) {
        LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();
        if(movie.getAuthor()!=null){
            queryWrapper.like(Movie::getAuthor,movie.getAuthor());
        }else
            throw new RuntimeException("传参错误！");
        List<Movie> list = movieService.list(queryWrapper);
        return list;
    }
}
